package com.tanhua.admin.service;

import cn.hutool.core.date.DateField;
import com.google.common.collect.Lists;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanhua.admin.mapper.AnalysisMapper;
import com.tanhua.admin.mapper.LogMapper;
import com.tanhua.admin.utils.ComputeUtil;
import com.tanhua.model.domain.Analysis;
import com.tanhua.model.vo.AnalysisGraphVo;
import com.tanhua.model.vo.AnalysisSummaryVo;
import com.tanhua.model.vo.DataPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DashboardService extends ServiceImpl<AnalysisMapper, Analysis> {

    @Autowired(required = false)
    private LogMapper logMapper;

    @Autowired(required = false)
    private AnalysisMapper analysisMapper;

    public AnalysisSummaryVo getSummary() {
        String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        //累计用户
        Analysis analysis = query().select("sum(num_registered) as numRegistered").one();
        Integer numRegistered = analysis.getNumRegistered();

        //今日新增、登录、活跃用户
        Analysis todayAnalysis = query().eq("record_date", today).one();

            Integer todayNumReg = todayAnalysis.getNumRegistered();
            Integer todayNumLogin = todayAnalysis.getNumLogin();
            Integer todayNumActive = todayAnalysis.getNumActive();


        //昨日新增、登录、活跃用户
        String yesterday = DateUtil.yesterday().toString("yyyy-MM-dd");
        Analysis yesterdayAnalysis = query().eq("record_date", yesterday).one();

            Integer yesterdayNumReg = yesterdayAnalysis.getNumRegistered();
            Integer yesterdayNumLogin = yesterdayAnalysis.getNumLogin();
            Integer yesterdayNumActive = yesterdayAnalysis.getNumActive();


        Long day7NumActive = analysisMapper.findNumActiveByDate(ComputeUtil.offsetDay(new Date(), -7), today);
        Long day30NumActive = analysisMapper.findNumActiveByDate(ComputeUtil.offsetDay(new Date(), -30), today);

        AnalysisSummaryVo vo = new AnalysisSummaryVo();
        vo.setCumulativeUsers(numRegistered.longValue());
        vo.setActivePassMonth(day30NumActive);
        vo.setActivePassWeek(day7NumActive);
        vo.setNewUsersToday(todayNumReg.longValue());
        vo.setNewUsersTodayRate(ComputeUtil.computeRate(todayNumReg, yesterdayNumReg));
        vo.setLoginTimesToday(todayNumLogin.longValue());
        vo.setLoginTimesTodayRate(ComputeUtil.computeRate(todayNumLogin, yesterdayNumLogin));
        vo.setActiveUsersToday(todayNumActive.longValue());
        vo.setActiveUsersTodayRate(ComputeUtil.computeRate(todayNumActive, yesterdayNumActive));

        return vo;
    }

    public AnalysisGraphVo displayGraph(Long sd, Long ed, Integer type) {
        DateTime startDate = DateUtil.date(sd);
        DateTime endDate = DateUtil.date(ed);
        DateTime lastYearSd = DateUtil.offset(startDate, DateField.YEAR, -1);
        DateTime lastYearEd = DateUtil.offset(endDate, DateField.YEAR, -1);
        AnalysisGraphVo analysisGraphVo = new AnalysisGraphVo();
        analysisGraphVo.setThisYear(this.getDataPointList(startDate, endDate, type));
        analysisGraphVo.setLastYear(this.getDataPointList(lastYearSd, lastYearEd, type));
        return analysisGraphVo;
    }

    public List<DataPoint> getDataPointList(DateTime sd, DateTime ed, Integer type) {
        String startDate = sd.toDateStr();
        String endDate = ed.toDateStr();
        List<Analysis> analysisList = query().between("record_date", startDate, endDate).list();
        List<DataPoint> dataPointList = new ArrayList<>();
        for (Analysis analysis : analysisList) {
            DataPoint dataPoint = new DataPoint();
            dataPoint.setTitle(analysis.getRecordDate().toString());
            if (type == 101) {
                //新增注册用户：num_registered
                dataPoint.setAmount(analysis.getNumRegistered());
            } else if (type == 102) {
                //活跃用户：num_active
                dataPoint.setAmount(analysis.getNumActive());
            } else if (type == 103) {
                //次日留存用户：num_retention1d
                dataPoint.setAmount(analysis.getNumRetention1d());
            }
            dataPointList.add(dataPoint);
        }
        return dataPointList;
    }


}
